package mongo.db.clients.firsts;

import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class Connect {

	/**
	 * @param args
	 * @throws UnknownHostException
	 */
	public static void main(String[] args) throws UnknownHostException {
		// TODO Auto-generated method stub

		MongoClient client = new MongoClient("localhost", 27017);

		DB db = client.getDB("murali");

		DBCollection collection = db.getCollection("murali");

		DBCursor iter = collection.find();

		// System.out.println(iter.count());

		/*
		 * Map<String, String> details = new HashMap<String, String>();
		 * details.put("name", "murali"); details.put("age", "32");
		 * details.put("dbname", "mongo");
		 */

		/*
		 * BasicDBObject obj = new BasicDBObject(details);
		 * 
		 * collection.insert(obj);
		 * 
		 * while ( iter.hasNext()) { System.out.println(iter.next()); }
		 */

		Random rn = new Random();

		DBCollection randoms = db.createCollection("traffic", null);

		for (int i = 0; i < 200; i++) {
			/*
			 * randoms.insert(new BasicDBObject("packets", (Object)
			 * rn.nextInt(1000) )); randoms.insert(new BasicDBObject("type",
			 * "outOctets" )); randoms.insert(new BasicDBObject("packets",
			 * (Object) rn.nextInt(1000) )); randoms.insert(new
			 * BasicDBObject("type", "inOctets" ));
			 */

			/*
			 * randoms.insert(new BasicDBObject("type", "inOctets")
			 * 
			 * .append("packets", (Object) rn.nextInt(1000)) .append("type",
			 * "outOctets") .append("packets", (Object) rn.nextInt(1000)));
			 */

			randoms.insert(new BasicDBObject("address", rn.nextInt(255) + "." + rn.nextInt(255) + "." + rn.nextInt(255) + "." + rn.nextInt(255)).append(
					"inPackets", rn.nextInt(1000)).append("outPackets", (Object) rn.nextInt(1000)));

			randoms.insert(new BasicDBObject("address", "210.210.124.153").append("inPackets", rn.nextInt(1000))
					.append("outPackets", (Object) rn.nextInt(1000)));

		}

		List<DBObject> flow = new LinkedList<DBObject>();

		for (int j = 0; j < 200; j++) {

			flow.add(new BasicDBObject("routerip", "202.144.74.180").append("routerid", 100).append("srcadr", "202.144.74.170")
					.append("dstadr", "202.144.74.180").append("inputint", "4").append("outputint", "5").append("octets", j * 54).append("srcport", "22")
					.append("dstport", "80").append("dstport", "80").append("protocl", "6").append("tos", "0").append("dstas", "0").append("direction", "0")
					.append("appid", "13"));
		}

		/*
		 * DBCollection traffic = db.getCollection("traffic"); DBCursor randIter
		 * = traffic.find();
		 */

		/*
		 * while(randIter.hasNext()) System.out.println(randIter.next());
		 */

		/*
		 * BasicDBObject query = new BasicDBObject("86", $gt 20);
		 * System.out.println(randoms.find(query).next());
		 */

		// create a output collection
		// DBCollection totalTraffic = db.createCollection("totalTraffic",
		// null);

		/*
		 * String map = "function() {emit ( this.this.inOctets)}"; String reduce
		 * = "function(key, value) {return Array.sum(value);}";
		 * 
		 * MapReduceCommand mpCommand = new MapReduceCommand(traffic, map,
		 * reduce, "totalTraffic", MapReduceCommand.OutputType.REPLACE, null);
		 * 
		 * MapReduceOutput mpOut = traffic.mapReduce(mpCommand);
		 * System.out.println("Result : " + mpOut.results());
		 * System.out.println("Output DB : " +
		 * db.getCollection("totalTraffic").find().count());
		 */

	}

}
